﻿using DevExpress.Utils.Win;
using DevExpress.XtraEditors.Popup;
using DevExpress.XtraGrid.Editors;
using DevExpress.XtraLayout;
using DXApplicationTangche.UC.生产计划.编排生产批.service;
using DXApplicationTangche.UC.面料审核.Service;
using DXApplicationTangche.UC.项目流程.Service;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DXApplicationTangche.UC.面料审核
{
    public partial class Form待审版一览 : DevExpress.XtraEditors.XtraForm
    {
        private String mnemonic { get; set; }
        private String empName { get; set; }

        private List<PreManufactureCADTodoDTO> preManufactureCADTodoDTOs = new List<PreManufactureCADTodoDTO>();
        public Form待审版一览()
        {
            InitializeComponent();
            this.searchLookUpEdit审版选择员工.Properties.DataSource = 任务Service.getAllStaffs();
            this.preManufactureCADTodoDTOs = 面料审核Service.getPreManufactureCADTodo();
            this.gridControl待审版一览.DataSource = this.preManufactureCADTodoDTOs;
            this.bandedGridView待审版一览.ExpandAllGroups();
        }

        private void textBox扫码生产批号_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyValue == 13)
            {
                编排生产批Service.generateProductionPlanAndSaveRecord(
                    "lijinjian01",
                    this.textBox扫码生产批号.Text.Trim(),
                    "00.0001",
                    ApsConsts.REVIEW_EXECUTE,
                    this.textBox扫码生产批号.Text.Trim()
                    );
                MessageBox.Show("审版完成");
                this.刷新();
            }
        }
        private void 刷新()
        {
            this.preManufactureCADTodoDTOs = 面料审核Service.getPreManufactureCADTodo();
            this.gridControl待审版一览.DataSource = this.preManufactureCADTodoDTOs;
            this.bandedGridView待审版一览.ExpandAllGroups();
        }
        #region
        private void searchLookUpEdit审版选择员工_Popup(object sender, EventArgs e)
        {
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                //得到取消按钮，重写点击事件
                Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                clearBtn.Click += clear审版选择员工Btn_Click;

                //添加一个simplebutton控件(确认按钮)
                LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                myLCI.TextVisible = false;
            }
        }
        private void searchLookUpEdit审版选择员工View_Click(object sender, EventArgs e)
        {
            int[] a = this.searchLookUpEdit审版选择员工.Properties.View.GetSelectedRows();
            if (a.Length > 0)
            {
                foreach (int rowHandle in a)
                {
                    this.mnemonic = this.searchLookUpEdit审版选择员工.Properties.View.GetRowCellValue(rowHandle, "mnemonic").ToString();
                    this.empName = this.searchLookUpEdit审版选择员工.Properties.View.GetRowCellValue(rowHandle, "name").ToString();
                }
            }
        }

        /// <summary>
        /// 清除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clear审版选择员工Btn_Click(object sender, EventArgs e)
        {
            this.searchLookUpEdit审版选择员工.ToolTip = null;
            searchLookUpEdit审版选择员工.EditValue = null;
        }
        private void searchLookUpEdit审版选择员工_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (null != e.Value)
            {
                e.DisplayText = this.empName;
            }
        }
        #endregion

        private void bandedGridView待审版一览_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            if (e.Column.Name.Equals("gridColumn完成标识"))
            {
                if ((Boolean)e.CellValue == true)
                {
                    e.Appearance.BackColor = Color.LightGreen;
                }
                else
                {
                    e.Appearance.BackColor = Color.LightSalmon;
                }
            }
        }
    }
}
